package views;

import controller.GameSettings;
import controller.GameState;
import controller.Manager2Gui;
import controller.human.Action;
import controller.human.CustomRole;
import controller.human.Person;
import controller.human.PhysicalState;
import controller.human.Role;
import controller.human.User;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import views.panes.RootPane;

import view.Gui2Manager;
import views.screens.dayScreens.DayScreen;

public final class MafiaFrame extends JFrame implements Gui2Manager {

    public GameSettings settings;
    UserData admin;
    public TreeSet<Person> players = new TreeSet<Person>();
    private final boolean debug = false;
    Manager2Gui manager;

    public MafiaFrame() {
        super("Mafia StoryTeller");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dim);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout(15, 10));
        update(new RootPane(this));
        repaint();
    }

    public void setCustomRoles(ArrayList<CustomRole> roles) {
        manager.setRoles(roles);

    }

    public void setDefaultRoles() {
        manager.setRoles(settings.numberPlayers);

    }

    public void update(JPanel s) {
        this.getContentPane().removeAll();
        this.getContentPane().invalidate();
        JPanel panel = new JPanel();
        panel.add(s);
        this.getContentPane().add(panel, BorderLayout.CENTER);
        if (debug) {
            this.setTitle("Mafia StoryTeller - " + s.getClass());
        }
        this.getContentPane().validate();
        this.repaint();
    }

    public void setGameSettings(GameSettings settings) {
        this.settings = settings;
        manager.setSettings(settings);
    }

    public void setAdmin(UserData adminis) {
        admin = adminis;
    }

    public Role addPlayer(Person player) {
        if (debug) {
            if (player.getName().equals("user4")) {
                player.setPhysicalState(PhysicalState.Dead);
            }
            if (player.getName().equals("user5")) {
                player.setPhysicalState(PhysicalState.Hand);
            }
        }
        this.players.add(player);
        Role role = manager.addUser(player);
        return role;
    }

    public void removePlayer(Person player) {
        players.remove(player);
    }

    @Override
    public void init(Manager2Gui manager) {
        this.manager = manager;
    }

    @Override
    public void dayResult(User killed, User handicapped, Action handicappAction) {
        update(new DayScreen(killed, handicapped, handicappAction, this));
    }

    @Override
    public void voteResults(Map<User, User> votes, User killed) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int nrPlayers() {
        int nr = 0;
        Iterator<Person> it = players.iterator();
        while (it.hasNext()) {
            Person p = it.next();
            if (p.getPhysicalState() != PhysicalState.Dead) {
                nr++;
            }
        }
        return nr;
    }

    public Person findUser(String name) {
        Iterator<Person> it = players.iterator();

        while (it.hasNext()) {
            Person p = it.next();
            if (p.getName().equals(name)) {
                return p;
            }
        }
        return null;
    }

    public void dayScreen() {
        if (manager.gameEnded() == 0) {
            manager.changeState(GameState.Day);
        } else {
            update(new ResultsScreen(this));
        }
    }

    public boolean addAction(String action1, String action2, String action3) {
        return manager.nightAction(findUser(action1), findUser(action2), (action3.equals("No_vote") ? Action.Hand : Action.Mute));
    }
}
